import { defineConfig, loadEnv } from 'vite'
import * as path from 'path'
import vuePlugin from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '')

  const alias = {
    '@': path.resolve(__dirname, './src'),
    'vue$': 'vue/dist/vue.runtime.esm-bundler.js',
  }

  return {
    base: env.VITE_BASE_STATIC_PATH, // index.html文件所在位置
    root: './', // js导入的资源路径，src
    resolve: {
      alias,
    },
    define: {
      'process.env': {}
    },
    server: {
      open: false,
      port: 8080,
      proxy: {
        '/admin': { // 需要代理的路径
          target: 'http://localhost:1204/',
          changeOrigin: true
        },
      },
    },
    build: {
      target: 'es2017',
      minify: 'terser', // 是否进行压缩, boolean | 'terser' | 'esbuild', 默认使用terser
      manifest: false, // 是否产出manifest.json
      sourcemap: false, // 是否产出sourcemap.json
      emptyOutDir: true,
      outDir: './dist', // 产出目录
    },
    plugins: [
      vuePlugin(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
        eslintrc: {
          enabled: true,
          filepath: ".eslintrc.json",
          globalsPropValue: true
        }
      }),
      Components({
        directoryAsNamespace: true,
        resolvers: [ElementPlusResolver()],
      }),
    ],
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
        }
      }
    },
  }
})
